package com.hknc.btscale.adapter;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;


import com.hknc.btscale.R;
import com.hknc.btscale.bean.TodayCheapBean;

import java.util.List;

/**
 * desc:
 * date:2021/11/22 11:45
 * author:sunguiyong
 */
public class AutoPollAdapter extends RecyclerView.Adapter<AutoPollAdapter.BaseViewHolder> {
    private final List<TodayCheapBean.DataBean> mData;

    public AutoPollAdapter(List<TodayCheapBean.DataBean> list) {
        this.mData = list;
    }

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_tejiamain, parent, false);
        BaseViewHolder holder = new BaseViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(BaseViewHolder holder, int position) {
        TodayCheapBean.DataBean dataBean = mData.get(position % mData.size());
        String priceTemp = String.valueOf(dataBean.getGoodsCurrentPrice());
        String price1 = "";
        String price2 = "";
        if (!TextUtils.isEmpty(priceTemp) && priceTemp.length() > 0) {
            price1 = priceTemp.substring(0, priceTemp.indexOf("."));
            price2 = priceTemp.substring(priceTemp.indexOf("."));

            holder.price.setText(price1);
            holder.price2.setText(price2);
        }
        holder.goosName.setText(dataBean.getGoodsName());
        holder.unit.setText("元/" + dataBean.getUnit());
    }

    @Override
    public int getItemCount() {
        return Integer.MAX_VALUE;
    }

    class BaseViewHolder extends RecyclerView.ViewHolder {
        TextView goosName, price, unit, price2;

        public BaseViewHolder(View itemView) {
            super(itemView);
            goosName = itemView.findViewById(R.id.goodsname_tv);
            price = itemView.findViewById(R.id.price_tv);
            unit = itemView.findViewById(R.id.unit_tv);
            price2 = itemView.findViewById(R.id.price_tv1);
        }
    }
}
